package com.ymttest.database.redis;

import java.util.HashMap;
import java.util.List;

import com.ymttest.database.mongo.util.SpringContextHolder;

import org.apache.commons.collections.CollectionUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

import java.util.Date;

public class RedisUtils {
	private static final JedisSentinelPool jedisSentinelPool;
    private static final String OK = "OK";

    static {
        jedisSentinelPool = SpringContextHolder.getBean(JedisSentinelPool.class);
    }

    /**
     * key不存在返回null
     * @param key
     * @return
     */
    public static String get(String key) {
        try (Jedis jedis = jedisSentinelPool.getResource()) {
            return jedis.get(key);
        }
    }

    /**
     * 不包含不存在的key
     * @param keys
     * @return
     */
    public static HashMap<String, String> mget(String... keys) {
        HashMap<String, String> hashMap = new HashMap<>();
        if(keys.length == 0){
            return hashMap;
        }
        List<String> values;
        try (Jedis jedis = jedisSentinelPool.getResource()) {
            values = jedis.mget(keys);
        }
        for (int i = 0; i < keys.length; i++) {
            if (values.get(i) == null) {
                continue;
            } else {
                hashMap.put(keys[i], values.get(i));
            }

        }

        return hashMap;
    }

    public static HashMap<String, String> mget(List<String> keys) {
        HashMap<String, String> hashMap = new HashMap<>();
        if(CollectionUtils.isEmpty(keys)){
            return hashMap;
        }
        String[] keyArray = new String[0];
        keyArray = keys.toArray(keyArray);
        return mget(keyArray);
    }

    public static void set(String key, String value) {
        try (Jedis jedis = jedisSentinelPool.getResource()) {
            if(!OK.equals(jedis.set(key, value))){
                throw new RuntimeException(String.format("redis set fail, key:%s, value:%s", key, value));
            }
        }
    }

    public static void set(String key, String value, Date expireTime) {
        try (Jedis jedis = jedisSentinelPool.getResource()) {
            if (!OK.equals(jedis.set(key, value))) {
                throw new RuntimeException(String.format("redis set fail, key:%s, value:%s", key, value));
            }
            jedis.expireAt(key, expireTime.toInstant().getEpochSecond());
        }
    }

    public static void set(String key, String value, int expireSeconds) {
        try (Jedis jedis = jedisSentinelPool.getResource()) {
            if (!OK.equals(jedis.set(key, value))) {
                throw new RuntimeException(String.format("redis set fail, key:%s, value:%s", key, value));
            }
            jedis.expire(key, expireSeconds);
        }
    }

    public static void expire(String key, int expireSeconds) {
        try (Jedis jedis = jedisSentinelPool.getResource()) {
            jedis.expire(key, expireSeconds);
        }
    }

    public static void expire(String key, Date expireTime) {
        try (Jedis jedis = jedisSentinelPool.getResource()) {
            jedis.expireAt(key, expireTime.toInstant().getEpochSecond());
        }
    }

    public static void delete(String key) {
        try (Jedis jedis = jedisSentinelPool.getResource()) {
            jedis.del(key);
        }
    }

    public static void delete(List<String> keys) {
        String[] arr = new String[0];
        arr = keys.toArray(arr);
        try (Jedis jedis = jedisSentinelPool.getResource()) {
            jedis.del(arr);
        }
    }

}
